﻿@page
@model FytSoa.Web.Pages.FytAdmin.Shop.CouponModel
@{
    ViewData["Title"] = "优惠券管理";
    Layout = AdminLayout.Pjax(HttpContext);
}
<div id="container">
    <div class="list-wall">
        <div class="layui-form list-search" style="margin-top:10px;">
            <button type="button" class="layui-btn layui-btn-sm" data-type="toolAdd"><i class="layui-icon"></i> 新增</button>
            <button type="button" class="layui-btn layui-btn-sm" data-type="toolDel"><i class="layui-icon"></i> 删除</button>
        </div>
        <table class="layui-hide" id="tablist" lay-filter="tool"></table>
    </div>
    <script>
        layui.config({
            base: '/themes/js/modules/'
        }).use(['table', 'layer', 'jquery', 'common'],
            function () {
                var table = layui.table,
                    layer = layui.layer,
                    $ = layui.jquery,
                    os = layui.common;
                table.render({
                    elem: '#tablist',
                    headers: os.getToken(),
                    url: '/api/shop/coupon/list',
                    cols: [
                        [
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'couponName', width: 260, title: '名称' },
                            {
                                field: 'couponTypes', title: '类型',width:100, templet: function (data) {
                                    return data.couponTypes == 1 ? '优惠券' :'运费券';
                                }
                            },
                            {
                                field: 'mode', width: 160, title: '使用方式', templet: function (data) {
                                    var str = '';
                                    if (data.mode === 1) {
                                        str = '立减';
                                    }
                                    if (data.mode === 2) {
                                        str = '打折';
                                    }
                                    if (data.mode === 3) {
                                        str = '返利';
                                    }
                                    return str;
                                }
                            },
                            { field: 'amount', title: '生成数量' },
                            { field: 'addTime', width: 200, title: '创建时间' },
                            { width: 220, title: '操作', templet: '#tool' }
                        ]
                    ],
                    page: true,
                    id: 'tables',
                    parseData: function (res) {
                        return {
                            "code": res.statusCode === 200 ? 0 : res.statusCode,
                            "msg": res.message,
                            "count": res.data.totalItems,
                            "data": res.data.items
                        };
                    }
                });

                var active = {
                    reload: function () {
                        table.reload('tables',
                            {
                                page: {
                                    curr: 1
                                }
                            });
                    },
                    toolAdd: function () {
                        os.Open('添加优惠券', '/fytadmin/shop/couponmodify', '1000px', '650px', function () {
                            active.reload();
                        });
                    },
                    toolDel: function () {
                        var checkStatus = table.checkStatus('tables')
                            , data = checkStatus.data;
                        if (data.length === 0) {
                            os.error("请选择要删除的项目~");
                            return;
                        }
                        var str = '';
                        $.each(data, function (i, item) {
                            str += item.guid + ",";
                        });
                        layer.confirm('确定要执行批量删除吗？', function (index) {
                            layer.close(index);
                            var loadindex = layer.load(1, {
                                shade: [0.1, '#000']
                            });
                            os.ajax('api/shop/coupon/delete', { parm: str }, function (res) {
                                layer.close(loadindex);
                                if (res.statusCode === 200) {
                                    active.reload();
                                    os.success('删除成功！');
                                } else {
                                    os.error(res.message);
                                }
                            });
                        });

                    }
                };
                $('.list-search .layui-btn').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });
                //监听工具条
                table.on('tool(tool)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'edit') {
                        os.Open('修改优惠券', '/fytadmin/shop/couponmodify/?guid=' + data.guid, '1000px', '650px', function () {
                            active.reload();
                        })
                    }
                    if (obj.event === 'look') {
                        os.Open('优惠券详情', '/fytadmin/shop/couponlog?guid='+data.guid, '1000px', '650px');
                    }
                });
            });
    </script>
    <script type="text/html" id="tool">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"><i class="layui-icon"></i> 修改</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="look"><i class="layui-icon layui-icon-templeate-1"></i> 查看券详情</a>
    </script>
</div>